package com.masterclient.app.modules.order.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.masterclient.app.R;
import com.masterclient.app.modules.order.beans.ConfirmCartBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.widget.ExpandTextView;

/**
 * 作者：sean
 * 创建时间：2017/8/4 17:24
 * 功能说明：
 */

public class CoupanRecycleAdapter extends BaseQuickAdapter<ConfirmCartBean.DataBean.ShSalesruleBean.ShSalesrulesBean.ShInvalidSalesrulesBean, BaseViewHolder> {
    public CoupanRecycleAdapter() {
        super(R.layout.item_coupon_new);
    }

    @Override
    protected void convert(BaseViewHolder helper, ConfirmCartBean.DataBean.ShSalesruleBean.ShSalesrulesBean.ShInvalidSalesrulesBean item) {
        helper.setText(R.id.tv_money,String.valueOf(item.getSh_discount_amount()));
        helper.setText(R.id.tv_num,String.valueOf(item.getSh_discount_amount()));
        helper.setText(R.id.tv_limit,item.getSh_name());
        helper.setText(R.id.tv_date,item.getSh_start_at()+"-"+item.getSh_end_at());
//        helper.setText(R.id.ext,item.getSh_description());

        RelativeLayout rl_top = helper.getView(R.id.rl_top);
        RelativeLayout rl_bottom = helper.getView(R.id.rl_bottom);
        CheckBox checkBox = helper.getView(R.id.checkBox);
//        ImageView iv_wave = helper.getView(R.id.iv_wave);

        if("by_percent".equals(item.getSh_simple_action())){
            helper.getView(R.id.ll_by_count).setVisibility(View.VISIBLE);
            helper.getView(R.id.ll_by_money).setVisibility(View.GONE);
        }else {
            helper.getView(R.id.ll_by_count).setVisibility(View.GONE);
            helper.getView(R.id.ll_by_money).setVisibility(View.VISIBLE);
        }

        helper.setText(R.id.tv_count,"可用"+item.getSh_num()+"次");

        ExpandTextView expandTextView = helper.getView(R.id.etv_text);
        RelativeLayout rlArrow = helper.getView(R.id.rl_arrow);
        ImageView iv_arrow = helper.getView(R.id.iv_arrow);
        expandTextView.setText(item.getSh_description());
        expandTextView.setTextSize(13);
        expandTextView.setTextColor(0xff666666);
        expandTextView.setTextLineSpacingExtra(ScreenUtils.dp2px(4));

        checkBox.setChecked(item.isCheck());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(onCheckChangeListerner!=null){
                    onCheckChangeListerner.onCheckChange(helper.getLayoutPosition(),isChecked);
                }
            }

        });
        helper.getView(R.id.rl_arrow).setOnClickListener(v->{
            if(iv_arrow.getTag() == null||(Boolean) iv_arrow.getTag()){
                expandTextView.expand();
                iv_arrow.setImageResource(R.mipmap.top_icon);
                iv_arrow.setTag(false);
            }else {
                expandTextView.shrink();
                iv_arrow.setImageResource(R.mipmap.down_icon);
                iv_arrow.setTag(true);
            }
        });

        expandTextView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(expandTextView.getLineCount()==1){
                    rlArrow.setVisibility(View.GONE);
                }else {
                    rlArrow.setVisibility(View.VISIBLE);
                }
            }
        }, 300);

        if(!item.isValid()){//不可选
            checkBox.setClickable(false);
            checkBox.setVisibility(View.INVISIBLE);
            helper.getView(R.id.rl_top).setBackgroundResource(R.mipmap.youhuiquan_bukexuan);
            if("1".equals(item.getSh_allow_additional_discount())){
                helper.getView(R.id.iv_add_salerule).setVisibility(View.VISIBLE);
                helper.getView(R.id.iv_add_salerule).setBackgroundResource(R.mipmap.img_bukediejia);
            }else {
                helper.getView(R.id.iv_add_salerule).setVisibility(View.GONE);
            }
        }else {
            checkBox.setClickable(true);
            checkBox.setVisibility(View.VISIBLE);
            helper.getView(R.id.rl_top).setBackgroundResource(R.mipmap.youhuiquan);
            if("1".equals(item.getSh_allow_additional_discount())){
                helper.getView(R.id.iv_add_salerule).setVisibility(View.VISIBLE);
                helper.getView(R.id.iv_add_salerule).setBackgroundResource(R.mipmap.img_kediejia);
            }else {
                helper.getView(R.id.iv_add_salerule).setVisibility(View.GONE);
            }
        }

        if(mData.size()-1 == helper.getLayoutPosition()){
            helper.getView(R.id.view_bottom).setVisibility(View.VISIBLE);
        }else {
            helper.getView(R.id.view_bottom).setVisibility(View.GONE);
        }
    }

    private OnCheckChangeListerner onCheckChangeListerner;

    public void setOnCheckChangeListerner(OnCheckChangeListerner onCheckChangeListerner) {
        this.onCheckChangeListerner = onCheckChangeListerner;
    }

    public interface OnCheckChangeListerner{
        void onCheckChange(int position,boolean check);
    }
}
